package com.bamanker.authclient.controller;

import com.bamanker.authclient.feign.ResClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * @author bamanker
 */
@RestController
@Slf4j
@RequiredArgsConstructor
public class ResourceController {

    private final ResClient resClient;

    /**
     * 使用feign调用
     */
    @GetMapping("/server/a/resource1")
    public Object getServerRes1(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient oAuth2AuthorizedClient) {
        String tokenValue = oAuth2AuthorizedClient.getAccessToken().getTokenValue();
        return resClient.getRes1("Bearer " + tokenValue);
    }

    @GetMapping("/server/a/resource2")
    public Object getServerRes2(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient oAuth2AuthorizedClient) {
        String tokenValue = oAuth2AuthorizedClient.getAccessToken().getTokenValue();
        return resClient.getRes2("Bearer " + tokenValue);
    }

    @GetMapping("/server/a/resource3")
    public Object getServerRes3(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient oAuth2AuthorizedClient) {
        String tokenValue = oAuth2AuthorizedClient.getAccessToken().getTokenValue();
        return resClient.getRes3("Bearer " + tokenValue);
    }

    @GetMapping("/server/a/publicResource")
    public Object getServerResPublic(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient oAuth2AuthorizedClient) {
        String tokenValue = oAuth2AuthorizedClient.getAccessToken().getTokenValue();
        return resClient.getPublicRes("Bearer " + tokenValue);
    }

    @GetMapping("/server/a/test01")
    public String getServerResTest01(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient oAuth2AuthorizedClient) {
        return getServer("http://127.0.0.1:9200/test01", oAuth2AuthorizedClient);
    }

    @GetMapping("/server/a/test02")
    public String getServerResTest02(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient oAuth2AuthorizedClient) {
        return getServer("http://127.0.0.1:9200/test02", oAuth2AuthorizedClient);
    }

    @GetMapping("/server/a/app")
    public String getServerResApp(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient oAuth2AuthorizedClient) {
        return getServer("http://127.0.0.1:9200/app", oAuth2AuthorizedClient);
    }

    @GetMapping("/server/a/web")
    public String getServerResWeb(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient oAuth2AuthorizedClient) {
        return getServer("http://127.0.0.1:9200/web", oAuth2AuthorizedClient);
    }

    /**
     * 使用webClient调用
     * 绑定token，请求微服务
     */
    private String getServer(String url, OAuth2AuthorizedClient oAuth2AuthorizedClient) {
        log.info("getServer");
        // 获取 access_token
        String tokenValue = oAuth2AuthorizedClient.getAccessToken().getTokenValue();

        // 发起请求"Bearer " + tokenValue
        Mono<String> stringMono = WebClient.builder()
                .defaultHeader("Authorization", "Bearer " + tokenValue)
                .build()
                .get()
                .uri(url)
                .retrieve()
                .bodyToMono(String.class);

        return stringMono.block();
    }
}
